<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="方案名称" prop="orderNo">
          <el-input
            v-model="queryParams.planName"
            placeholder="请输入方案名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="addfangan"
            >新增佣金方案</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="list" border>
        <el-table-column
          label="方案名称"
          min-width="160"
          align="center"
          prop="planName"
          show-overflow-tooltip
        />
        <el-table-column
          label="方案编号"
          min-width="160"
          align="center"
          prop="planNo"
          show-overflow-tooltip
        />
        <el-table-column
          label="一级分佣金额(元)"
          min-width="180"
          align="center"
          prop="primaryCommissionAmount"
          show-overflow-tooltip
        />
        <el-table-column
          label="一级分佣金百分比(%)"
          min-width="180"
          align="center"
          prop="primaryCommissionPercentage"
          show-overflow-tooltip
        />
        <el-table-column
          label="二级分佣金额(元)"
          min-width="180"
          align="center"
          prop="secondaryCommissionAmount"
          show-overflow-tooltip
        />
        <el-table-column
          label="二级分佣金百分比(%)"
          min-width="180"
          align="center"
          prop="secondaryCommissionPercentage"
          show-overflow-tooltip
        />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建人"
          min-width="180"
          align="center"
          prop="createName"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作"
          min-width="180"
          align="center"
          class-name="small-padding fixed-width"
          fixed="left"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handDetail(scope.row)"
              >查看</el-button
            >
            <el-button size="mini" type="text" @click="handEdit(scope.row)"
              >修改</el-button
            >
            <el-button size="mini" type="text" @click="handDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 新增方案 -->
    <el-dialog
      :title="title"
      width="600px"
      :visible.sync="showxinzheng"
      @close="resetFrom"
    >
      <el-form label-width="80px" :model="form" :rules="rules" ref="form">
        <el-form-item label="方案名称" prop="planName">
          <el-input v-model="form.planName" placeholder="方案名称"></el-input>
        </el-form-item>
        <el-form-item label="一级佣金" prop="primaryAmountChecked">
          <el-row>
            <el-col :span="12">
              <el-select
                v-model="form.primaryAmountChecked"
                @change="primaryAmount"
                style="width: calc(100% - 6px)"
              >
                <el-option label="按固定金额" :value="1"></el-option>
                <el-option label="按百分比" :value="2"></el-option>
              </el-select>
            </el-col>
            <el-col :span="12">
              <!-- 按固定金额 -->
              <template v-if="form.primaryAmountChecked == 1">
                <el-input
                  v-model="form.primaryCommissionAmount"
                  style="width: calc(100% - 6px); margin-left: 6px"
                  placeholder="一级分销佣金金额（最多两位小数）"
                >
                  <div slot="suffix" style="margin-right: 8px">元</div>
                </el-input>
              </template>
              <!-- 按百分比 -->
              <template v-if="form.primaryAmountChecked == 2">
                <el-input
                  v-model="form.primaryCommissionPercentage"
                  style="width: calc(100% - 6px); margin-left: 6px"
                  placeholder="一级分销佣金百分比（最多两位小数）"
                >
                  <div slot="suffix" style="margin-right: 8px">%</div>
                </el-input>
              </template>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="二级佣金" prop="secondaryAmountChecked">
          <el-row>
            <el-col :span="12">
              <el-select
                v-model="form.secondaryAmountChecked"
                @change="secondaryAmount"
                style="width: calc(100% - 6px)"
              >
                <el-option label="按固定金额" :value="1"></el-option>
                <el-option label="按百分比" :value="2"></el-option>
              </el-select>
            </el-col>
            <el-col :span="12">
              <!-- 按固定金额 -->
              <template v-if="form.secondaryAmountChecked == 1">
                <el-input
                  v-model="form.secondaryCommissionAmount"
                  placeholder="二级分销佣金金额（最多两位小数）"
                  style="width: calc(100% - 6px); margin-left: 6px"
                >
                  <div slot="suffix" style="margin-right: 8px">元</div>
                </el-input>
              </template>
              <!-- 按百分比 -->
              <template v-if="form.secondaryAmountChecked == 2">
                <el-input
                  v-model="form.secondaryCommissionPercentage"
                  style="width: calc(100% - 6px); margin-left: 6px"
                  placeholder="二级分销佣金百分比（最多两位小数）"
                >
                  <div slot="suffix" style="margin-right: 8px">%</div>
                </el-input>
              </template>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addsave" :loading="addfanganLoading"
          >保 存</el-button
        >
        <el-button @click="showxinzheng = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看详情 -->
    <el-dialog
      title="查看详情"
      width="900px"
      :visible.sync="showxiangiqng"
      @close="xiangqing"
    >
      <el-descriptions border>
        <el-descriptions-item label-class-name="my-descriptions-label"  :span="2" label="方案名称">{{
          xiangqing.planName
        }}</el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  :span="2" label="方案编号">{{
          xiangqing.planNo
        }}</el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  :span="2" label="一级分佣金额(元)">{{
          xiangqing.primaryCommissionAmount || 0
        }}</el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  :span="2" label="一级分佣百分比(%)">{{
          xiangqing.primaryCommissionPercentage || 0
        }}</el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  :span="2" label="二级分佣金额(元)">{{
          xiangqing.secondaryCommissionAmount || 0
        }}</el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  :span="2" label="二级分佣百分比(%)">{{
          xiangqing.secondaryCommissionPercentage || 0
        }}</el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  :span="2" label="创建人名称">{{
          xiangqing.createName
        }}</el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  :span="2" label="创建时间">{{
          xiangqing.createTime
        }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listMkOrder, exportMkOrder } from '@/api/marketing/mkOrder'
import * as CommissionApi from '@/api/marketing/commission.js'

export default {
  name: 'MkOrder',
  data() {
    return {
      title: '',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 全名营销订单表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      addfanganLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTime: '', // 创建时间
        planName: '', // 方案名称
      },
      showxinzheng: false,
      // 表单参数
      form: {
        planName: '', // 方案名称
        primaryAmountChecked: 1, // 一级分销佣金:1按固定金额、2按百分比
        primaryCommissionAmount: 0, // 一级分销佣金金额
        primaryCommissionPercentage: 0, // 一级分销佣金百分比
        secondaryAmountChecked: 1, // 二级分销佣金:1按固定金额、2按百分比
        secondaryCommissionAmount: 0, // 二级分销佣金金额
        secondaryCommissionPercentage: 0, // 	二级分销佣金百分比
      },
      // 表单校验
      rules: {
        planName: [
          { required: true, message: '请输入方案名称', trigger: 'blur' },
        ],
        primaryAmountChecked: [
          { required: true, message: '请输入一级分销佣金', trigger: 'blur' },
        ],
        secondaryAmountChecked: [
          { required: true, message: '请输入二级分销佣金', trigger: 'blur' },
        ],
      },
      isEdit: false,
      showxiangiqng: false,
      xiangqing: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {
    primaryAmount() {
      this.form.primaryCommissionAmount = 0
      this.form.primaryCommissionPercentage = 0
    },
    secondaryAmount() {
      this.form.secondaryCommissionAmount = 0
      this.form.secondaryCommissionPercentage = 0
    },
    /** 查询全名营销订单列表 */
    getList() {
      this.loading = true
      CommissionApi.list(this.queryParams).then((response) => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.addfanganLoading = false
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        createTime: '', // 创建时间
        planName: '', // 方案名称
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      this.handleQuery()
    },
    /** 详情按钮操作 */
    handDetail(row) {
      CommissionApi.detail(row.commissionPlanId).then((res) => {
        this.showxiangiqng = true
        this.xiangqing = res.data
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有全名营销订单数据项？')
        .then(() => {
          this.exportLoading = true
          return exportMkOrder(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
    /**
     * 创建方案
     */
    addfangan() {
      this.title = '新增佣金方案'
      this.showxinzheng = true
    },
    /**
     * 提交创建方案
     */
    addsave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.addfanganLoading = true
          if (this.isEdit) {
            CommissionApi.put(this.form)
              .then((res) => {
                this.addfanganLoading = false
                this.$message.success(res.msg)
                this.showxinzheng = false
                this.handleQuery()
              })
              .catch((err) => {
                // this.$message.error('添加失败')
                this.addfanganLoading = false
              })
            return
          }
          CommissionApi.add(this.form)
            .then((res) => {
              this.addfanganLoading = false
              this.$message.success(res.msg)
              this.showxinzheng = false
              this.handleQuery()
            })
            .catch((err) => {
              //this.$message.error('添加失败')
              this.addfanganLoading = false
            })
        }
      })
    },
    resetFrom() {
      this.form = {
        planName: '', // 方案名称
        primaryAmountChecked: 1, // 一级分销佣金:1按固定金额、2按百分比
        primaryCommissionAmount: 0, // 一级分销佣金金额
        primaryCommissionPercentage: 0, // 一级分销佣金百分比
        secondaryAmountChecked: 1, // 二级分销佣金:1按固定金额、2按百分比
        secondaryCommissionAmount: 0, // 二级分销佣金金额
        secondaryCommissionPercentage: 0, // 	二级分销佣金百分比
      }
      this.isEdit = false
    },
    /**
     * 编辑
     */
    handEdit(row) {
      this.form = row
      this.showxinzheng = true
      this.title = '编辑佣金方案'
      this.isEdit = true
    },
    /** 各种状态弹窗操作 */
    handleConfirm(data, callback) {
      this.$modal.confirm(data.msg).then((e) => {
        callback()
      })
    },
    /**
     * 删除
     */
    handDel(row) {
      this.handleConfirm({ msg: '确定要删除佣金方案？' }, (e) => {
        CommissionApi.del(row.commissionPlanId).then((res) => {
          this.$message.success(res.msg)
          this.showxinzheng = false
          // this.handleQuery();
          // this.handleQuery();
          this.getList()
        })
      })
    },
  },
}
</script>
